package com.fanhao.dllo.soundfm.activity;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.utils.PlayDialog;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/7.
 * 基类
 * 主要简化了findViewById和两个抽象方法:setLayout(),initView();
 */
public abstract class BaseActivity extends AppCompatActivity {
    private FragmentManager fm;
    private FragmentTransaction ft;
    private PlayDialog dialog;
    private ImageLoader imageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayout());
//        setTheme(R.style.AppTheme);
        dialog = new PlayDialog(this, R.anim.loading_dialog);
        // 防止点击界面dialog消失
        dialog.setCanceledOnTouchOutside(false);
        initView();
    }

    /**
     * 简化findViewById
     *
     * @param resId
     * @param <T>
     * @return
     */
    protected <T extends View> T findView(int resId) {
        T t = (T) findViewById(resId);
        return t;
    }

    /**
     * 加载布局文件的抽象方法
     **/
    protected abstract int setLayout();

    /**
     * 初始化的抽象方法
     **/
    protected abstract void initView();

    // 初始化数据
    protected void initData() {}

    // 显示waitDialog
    public void showDialog(){
        dialog.show();}

    // 隐藏waitDia
    public void dismissDialog(){dialog.dismiss();}


    // Toast提示
    protected void Toast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    // 获得请求时imageLoader对象的方法
    public ImageLoader getImageLoader(){
        imageLoader = VolleySingleton.getInstance()._getImageLoader();
        return imageLoader;
    }
    // 替换Fragment
    protected void replaceFragment(int id,Fragment fragment) {
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(id,fragment).addToBackStack(null);
        ft.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
